package patterns.proxyPattern;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import patterns.compositePattern.Department;
import patterns.compositePattern.Staff;

public class IACAProxy implements IACA {
	private static ConcurrentHashMap<String, Department> departmentCache;
	private static ConcurrentHashMap<String, List<Staff>> staffCache;
	private static IACA realIaca;

	@Override
	public Department queryDepartment(String name) {
		// if (departmentCache != null) {
		// if (departmentCache.get(name) != null) {
		// System.out.println("from cache");
		// return departmentCache.get(name);
		// } else {
		// Department department = realIaca.queryDepartment(name);
		// System.out.println("from real");
		// departmentCache.put(name, department);
		// return department;
		// }
		// } else {
		// departmentCache = new ConcurrentHashMap<>();
		// realIaca = new ChengDuIACA();
		// Department department = realIaca.queryDepartment(name);
		// System.out.println("from real");
		// departmentCache.put(name, department);
		// return department;
		// }
		if (realIaca != null) {
			if (departmentCache == null) {
				departmentCache = new ConcurrentHashMap<>();
				Department department = realIaca.queryDepartment(name);
				System.out.println("from real");
				departmentCache.put(name, department);
				return department;
			} else {
				if (departmentCache.get(name) != null) {
					System.out.println("from cache");
					return departmentCache.get(name);
				} else {
					Department department = realIaca.queryDepartment(name);
					System.out.println("from real");
					departmentCache.put(name, department);
					return department;
				}
			}
		} else {
			departmentCache = new ConcurrentHashMap<>();
			realIaca = new ChengDuIACA();
			Department department = realIaca.queryDepartment(name);
			System.out.println("from real");
			departmentCache.put(name, department);
			return department;
		}
	}

	private List<Staff> queryFromDptCacheAndPushStaffCache(String name) {
		if (departmentCache.get(name) != null) {
			List<Staff> staffs = departmentCache.get(name).getAllStaffEntity();
			System.out.println("from dpt cache");
			staffCache.put(name, staffs);
			return staffs;
		} else {
			List<Staff> staffs = realIaca.queryStaffOfDepartment(name);
			System.out.println("from real");
			staffCache.put(name, staffs);
			return staffs;
		}
	}

	private List<Staff> judgeAndQueryFromDptCacheAndPushStaffCache(String name) {
		if (departmentCache != null && departmentCache.get(name) != null) {
			List<Staff> staffs = departmentCache.get(name).getAllStaffEntity();
			System.out.println("from dpt cache");
			staffCache.put(name, staffs);
			return staffs;
		} else {
			List<Staff> staffs = realIaca.queryStaffOfDepartment(name);
			System.out.println("from real");
			staffCache.put(name, staffs);
			return staffs;
		}
	}

	@Override
	public List<Staff> queryStaffOfDepartment(String name) {
		// TODO Auto-generated method stub
		if (realIaca != null) {
			if (staffCache == null) {
				staffCache = new ConcurrentHashMap<>();
				return queryFromDptCacheAndPushStaffCache(name);
			} else {
				if (staffCache.get(name) != null) {
					System.out.println("from cache");
					return staffCache.get(name);
				} else {
					return judgeAndQueryFromDptCacheAndPushStaffCache(name);
				}
			}
		} else {
			staffCache = new ConcurrentHashMap<>();
			realIaca = new ChengDuIACA();
			List<Staff> staffs = realIaca.queryStaffOfDepartment(name);
			System.out.println("from real");
			staffCache.put(name, staffs);
			return staffs;
		}
	}

}
